package com.itheima.controller;


import com.itheima.common.R;
import com.itheima.pojo.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private HttpServletRequest request;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @GetMapping("/list")
    public R list() {
        R r = shoppingCartService.findByUserId();
        return r;
    }

    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart) {
        HttpSession session = request.getSession();
        Long userId = (Long) session.getAttribute("userId");
        R r = shoppingCartService.add(shoppingCart, userId);
        return r;
    }

    // 清空购物车
    @DeleteMapping("/clean")
    public R clean() {
        shoppingCartService.clean();
        return R.success("清空成功");
    }

    @PostMapping("/sub")
    public R sub(@RequestBody ShoppingCart shoppingCart ){
        shoppingCartService.sub(shoppingCart);
        return R.success("删除购物车成功");
    }
}
